*WHAT'S ACROSS THE BORDER?
*JHA, NEUMARK, AND RODRIGUEZ-LOPEZ

include directories.do


***********************************************
*Rolling regressions

***********************************************

foreach u in "czonestate" {
* Load stacked pair data at the county or czone-state level
use "`datadir'`s'cbp_stacked_`u'pair_sample.dta", clear

************************************************
*******COUNTY-PAIRS OR MULTI-STATA CZONE PAIRS
************************************************

*** PAIR ID VARIABLE (EITHER CZONESTATEPAIR OR COUNTYPAIR)
local pair `u'pair


*Entity variable (EITHER CZONESTATE OR COUNTY)
local geo `u'

***********************************************
*GENERATE VARIABLES (SAME FOR COUNTY OR CZONE)
***********************************************


gen lmw=log(mw)

*Generate totempm, which removes emp of the industry of interest
foreach b in "emp" {
gen tot`b'm=tot`b'-`b'
}

foreach b in "emp" "wageap" {

gen l`b'=log(`b')
gen ltot`b'=log(tot`b')
gen ltot`b'm=log(tot`b'm)
}

gen lworkagepop=log(workagepop)


********************************************
**** DEFINE LOCAL VARIABLES
********************************************

local period year

*Dependent variable and regressors (SAME FOR COUNTY OR CZONE)

local mwvar lmw
local popvar lworkagepop
local wagevar lwageap
local empvar lemp
local totempvar ltotempm
local totwagevar ltotwageapm


**************************************************
****STACKED REGRESSIONS WITH REGHDFE
**************************************************

sort  `pair' `period'
 
gen state_a = statea
gen state_b = stateb

gen st_min = min(state_a, state_b)
gen st_max = max(state_a, state_b)
egen bordersegment = group(st_min st_max)
egen pairperiod=group(`pair' `period')


*Weighting

forval j=1990/2000 {
gen emp`j'=emp if year==`j'
gen pop`j'=workagepop if year==`j'
if "`u'" == "czonestate" {
bys sic87xx czone state: egen emp`j'_=mean(emp`j')
bys sic87xx czone state: egen pop`j'_=mean(pop`j')
	}
else {
bys sic87xx county: egen emp`j'_=mean(emp`j')
bys sic87xx county: egen pop`j'_=mean(pop`j')
}
drop emp`j' pop`j'
ren emp`j'_ emp`j'
ren pop`j'_ pop`j'
gen lemp`j'=log(emp`j')
gen lpop`j'=log(pop`j')

}


*Windows

forval j=1990/2000 {
local awemp`j' "[aw = emp`j']"
local awpop`j' "[aw = pop`j']"
}

*START IN 1990
forval j=2006/2016 {
local window`j' "`period'>=1990 & `period'<=`j'"

*Specify clustering
local options "vce(cluster state bordersegment) absorb(`u' pairperiod)"

*Specifications

foreach h in "emp" "wage" {

local controls "`tot`h'var' `popvar'"
local entityfe "i.`u'"

eststo l0_`h'_`j': reghdfe ``h'var' `mwvar' `controls' if (`window`j''), `options'

eststo l1_`h'_`j': reghdfe ``h'var' `mwvar' `controls' if (`window`j'') `awemp1990', `options'

eststo l2_`h'_`j': reghdfe ``h'var' `mwvar' `controls' if (`window`j'') `awpop1990', `options'



}

}

}

/* Print results */

tempfile specs

#delimit ;
esttab l0_emp* using "`specs'.csv",
	nodepvars
	collabels(, none)
	mlabels(, none)
	b(%9.3f) se(%9.3f)
	star(* .10 ** .05 *** .01)
	title("Employment - unweighted")
	keep(`mwvar')
	nonotes
	label
	noobs	
	replace;
#delimit cr

#delimit ;
esttab l1_emp* using "`specs'.csv",
	nodepvars
	collabels(, none)
	mlabels(, none)
	b(%9.3f) se(%9.3f)
	star(* .10 ** .05 *** .01)
	title("Employment - emp weighted")
	keep(`mwvar')
	nonotes
	label
	noobs	
	append;
#delimit cr

#delimit ;
esttab l2_emp* using "`specs'.csv",
	nodepvars
	collabels(, none)
	mlabels(, none)
	b(%9.3f) se(%9.3f)
	star(* .10 ** .05 *** .01)
	title("Employment - pop weighted")
	keep(`mwvar')
	nonotes
	label
	noobs	
	append;
#delimit cr

#delimit ;
esttab l0_wage* using "`specs'.csv",
	nodepvars
	collabels(, none)
	mlabels(, none)
	b(%9.3f) se(%9.3f)
	star(* .10 ** .05 *** .01)
	title("Earnings - unweighted")
	keep(`mwvar')
	nonotes
	label
	noobs	
	append;
#delimit cr

#delimit ;
esttab l1_wage* using "`specs'.csv",
	nodepvars
	collabels(, none)
	mlabels(, none)
	b(%9.3f) se(%9.3f)
	star(* .10 ** .05 *** .01)
	title("Earnings - emp weighted")
	keep(`mwvar')
	nonotes
	label
	noobs	
	append;
#delimit cr

#delimit ;
esttab l2_wage* using "`specs'.csv",
	nodepvars
	collabels(, none)
	mlabels(, none)
	b(%9.3f) se(%9.3f)
	star(* .10 ** .05 *** .01)
	title("Earnings - pop weighted")
	keep(`mwvar')
	nonotes
	label
	noobs	
	append;
#delimit cr

* Switch the output to Excel format
insheet using "`specs'.csv", comma clear
export excel "`outdir'`s'TablesJNR.xlsx", sheetreplace sheet(Table5)


clear all



